package com.yc;

import com.yc.commons.DBHelper;
import com.yc.commons.DbProperties;
import com.yc.email.EmailProperties;
import com.yc.util.ISystemInfoUtil;
import com.yc.util.SystemInfoUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * @author zwei
 * @create 2024-08-18 16:16
 */

@Configuration
@EnableConfigurationProperties({SystemInfoPro.class, DbProperties.class, EmailProperties.class})
@ConditionalOnClass({SystemInfoPro.class, ISystemInfoUtil.class,})
public class SystemInfoAutoConfiguration {
    //业务类的bean
    @Bean
    @ConditionalOnMissingBean({ISystemInfoUtil.class})  //spring容器中没有这个bean的时候 才会创建这个bean
    public ISystemInfoUtil systemInfoUtil() {
        System.out.println("实例化 ISystemInfoUtil,并托管");
        return new SystemInfoUtil();
    }

    @Bean
    @ConditionalOnClass({java.sql.Driver.class})    //spring容器中有这个类的时候 才会创建这个bean
    public DBHelper dbHelper() {
        return new DBHelper();
    }

    @Bean
    @ConditionalOnClass(name = "org.springframework.mail.javamail.JavaMailSender")
    public JavaMailSender javaMailSender(@Value("${yc.email.host}}") String host,
                                         @Value("${yc.email.port}") int port,
                                         @Value("${yc.email.auth}") String auth,
                                         @Value("${yc.email.username}") String username,
                                         @Value("${yc.email.password}") String password,
                                         @Value("${yc.email.from}") String from
    ) {
        System.out.println("实例化 JavaMailSender,并托管");
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(host);
        javaMailSender.setUsername(username);
        javaMailSender.setPassword(password);
        javaMailSender.setPort(port);
        Properties p= javaMailSender.getJavaMailProperties();
        // 设置邮件发送的认证方式为开启
        p.put("mail.smtp.auth", auth);
        // 设置邮件传输协议为SMTP
        p.put("mail.transport.protocol", "smtp");
        // 设置支持的SSL协议版本
        p.put("mail.smtp.ssl.protocols", "TLSv1.2");
        // 根据端口号判断是否需要开启TLS
        if (port==587){
            p.put("mail.smtp.starttls.enable", "true");
        }
        // 根据端口号判断是否需要配置SSL
        if (port==465){
            p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            p.put("mail.smtp.socketFactory.port", "465");
            p.put("spring.mail.properties.mail.smtp.ssl.enable", "true");
        }
        // 返回配置好的JavaMailSenderImpl对象，作为bean
        return javaMailSender;
    }
}
